package com.helen_dragon.healthsystem.controller;/*
 *@Author: helen
 *@Date:   2021/6/6 10:50
 *@Description:
 */

import com.helen_dragon.healthsystem.model.DynamicSql;
import com.helen_dragon.healthsystem.model.information;
import com.helen_dragon.healthsystem.model.person;
import com.helen_dragon.healthsystem.service.AdminService;
import com.helen_dragon.healthsystem.service.personService;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/admin")

public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private personService perService;


    @RequestMapping("/ToPage/{pageName}")
    public String ToPage(@PathVariable("pageName") String pageName){
        return pageName;
    }


    @RequestMapping("/login")
    public String login(@ModelAttribute person per, Model model){
        person person2 = perService.selectPerson(per.getPersonId());
        String personId = per.getPersonId();
        String password = per.getPassword();
        if(person2 != null){
            if(per.getPassword().equals(person2.getPassword())){
                if(person2.getFlag()==1)
                    return "redirect:/admin/personList";
                else {
                    model.addAttribute("NotAdmin", "您不是管理员");
                    return "AdminLogin";
                }
            }
            else {
                String str= "密码错误，请重新输入！";
                model.addAttribute("personId", personId);
                model.addAttribute("password", password);
                model.addAttribute("passwordError", str);
                return "AdminLogin";
            }
        }
        else {
            String str= "该账号尚未注册，请先注册";
            model.addAttribute("personId", personId);
            model.addAttribute("password", password);
            model.addAttribute("passwordError", str);
            model.addAttribute("userError", str);
            return "AdminLogin";
        }
    }


    @RequestMapping("/personList")
    public String personList(Model model){
        List<person> list =adminService.getAllPerson();
        model.addAttribute("personList", list);
        return "person";
    }

    @RequestMapping("/addAdmin")
    public String addAdmin(@ModelAttribute person per, Model model){
        int i = adminService.addAdmin(per);
        if(i == 1){
            return "redirect:/admin/personList";
        }
        else {
            model.addAttribute("name",per.getName());
            model.addAttribute("personId",per.getPersonId());
            model.addAttribute("password",per.getPassword());
            if(perService.selectPerson(per.getPersonId()).getFlag() == 0)
                model.addAttribute("userExists",per.getPersonId() + "为普通用户\n可在列表编辑升级为管理员用户!");
            else
                model.addAttribute("userExists", per.getPersonId() + "用户已存在，并且是管理员用户");
            return "inputAdmin";
        }
    }

    @RequestMapping("/editPerson/{personId}")
    public String editPerson(@PathVariable String personId, Model model){
        final person person = perService.selectPerson(personId);
        model.addAttribute("person", person);
        return "editPerson";
    }

    @RequestMapping("/updatePerson")
    public String updatePerson(@ModelAttribute person per){
        adminService.updatePerson(per);
        return "redirect:/admin/personList";
    }

    @RequestMapping("/deletePerson/{personId}")
    public String deletePerson(@PathVariable String personId){
        adminService.deletePerson(personId);
        return "redirect:/admin/personList";
    }

    @RequestMapping("/getDate")
    public String getDate(Model model){
        final List<information> informationList = adminService.getDate();
        model.addAttribute("informationList", informationList);
        model.addAttribute("dynamicSql", new DynamicSql());
        return "collection";
    }

    @RequestMapping("/selectByCondition")
    public String selectByCondition(@ModelAttribute DynamicSql dynamicSql, Model model){
        List<information> informationList = adminService.selectByCondition(dynamicSql);
        model.addAttribute("informationList", informationList);
        model.addAttribute("dynamicSql", dynamicSql);
        return "collection";

    }

    @RequestMapping("/getTodayNotSubmit")
    public String getTodayNotSubmit(Model model){
        List<person> todayNotSubmit = adminService.getTodayNotSubmit();
        model.addAttribute("todayNotSubmit",todayNotSubmit);
        return "TodayNotSubmitPersons";
    }

    @RequestMapping("/downloadInformation")
    public String downloadInformation(@ModelAttribute DynamicSql dynamicSql, HttpServletResponse response, Model model) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("打卡信息表");
        List<information> informationExcelDownloads = adminService.selectByCondition(dynamicSql);
        String fileName = "information" + ".xls";
        int rowNum = 1;
        String [] headers = {"name", "person_id", "birthday", "CD_type", "CD_card", "identity",
                "phone", "native_city", "home_address", "now_address", "health", "key_area", "key_people",
                "temperature", "time"};
        HSSFRow row = sheet.createRow(0);
        for (int i = 0;i<headers.length;i++){
            HSSFCell cell = row.createCell((short) i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        for (information info : informationExcelDownloads){
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell((short) 0).setCellValue(new HSSFRichTextString(info.getName()));
            row1.createCell((short) 1).setCellValue(new HSSFRichTextString(info.getPersonId()));
            row1.createCell((short) 2).setCellValue(new HSSFRichTextString(info.getBirthday().toString()));
            row1.createCell((short) 3).setCellValue(new HSSFRichTextString(info.getCdType()));
            row1.createCell((short) 4).setCellValue(new HSSFRichTextString(info.getCdCard()));
            row1.createCell((short) 5).setCellValue(new HSSFRichTextString(info.getIdentity()));
            row1.createCell((short) 6).setCellValue(new HSSFRichTextString(info.getPhone()));
            row1.createCell((short) 7).setCellValue(new HSSFRichTextString(info.getNativeCity()));
            row1.createCell((short) 8).setCellValue(new HSSFRichTextString(info.getHomeAddress()));
            row1.createCell((short) 9).setCellValue(new HSSFRichTextString(info.getNowAddress()));
            row1.createCell((short) 10).setCellValue(new HSSFRichTextString(info.getHealth()));
            row1.createCell((short) 11).setCellValue(new HSSFRichTextString(info.getKeyArea()));
            row1.createCell((short) 12).setCellValue(new HSSFRichTextString(info.getKeyPeople()));
            row1.createCell((short) 13).setCellValue(new HSSFRichTextString(String.valueOf(info.getTemperature())));
            row1.createCell((short) 14).setCellValue(new HSSFRichTextString(String.valueOf(info.getTime()).replace('T', ' ')));
            rowNum++;

        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
        return "redirect:/admin/personList";
    }


}
